Ext.define('UmlWebEditor.tests.WorkSpaceTest', {
	extend: 'UmlWebEditor.tests.Test',
	requires: [
		'UmlWebEditor.view.WorkSpace',
		'UmlWebEditor.controller.WorkSpaceController'
	],
	tests: {
		createWorkSpaceTest: function () {
			var controller = Ext.UmlWebEditor.getController('WorkSpaceController');
			verify(controller.self.getName() == 'UmlWebEditor.controller.WorkSpaceController', 'WorkSpaceController not created');

			var isWorkSpaceWidgetCreated = false;
			var onWorkSpaceWidgetRender = controller.onWorkSpaceWidgetRender;
			controller.onWorkSpaceWidgetRender = function(view) {
				isWorkSpaceWidgetCreated = true;
				verify(view.self.getName() == 'UmlWebEditor.view.WorkSpace', 'view != WorkSpace');
			};

			var workSpaceWidget = Ext.create('UmlWebEditor.view.WorkSpace', {
				renderTo: Ext.getBody()
			});
			verify(workSpaceWidget.self.getName() == 'UmlWebEditor.view.WorkSpace', 'WorkSpace not created');

			verify(isWorkSpaceWidgetCreated, 'onWorkSpaceWidgetRender not called');

			//back old handlers
			controller.onWorkSpaceWidgetRender = onWorkSpaceWidgetRender;

			workSpaceWidget.destroy();
		},
		createCollectionTest: function () {
			var controller = Ext.UmlWebEditor.getController('WorkSpaceController');

			var isCollectionCreated = false;
			var onCollectionRender = controller.onCollectionRender;
			controller.onCollectionRender = function(view) {
				console.log('createMainToolBarTest: onCollectionRender');
				isCollectionCreated = true;
				verify(view.self.getName() == 'UmlWebEditor.view.ToolsCollection', 'view != ToolsCollection');
			};

			var workSpaceWidget = Ext.create('UmlWebEditor.view.WorkSpace', {
				renderTo: Ext.getBody()
			});
			verify(isCollectionCreated, 'onCollectionRender not called');
			//back old handlers
			controller.onCollectionRender = onCollectionRender;

			workSpaceWidget.destroy();
		},
		createToolBarTest: function () {
			var controller = Ext.UmlWebEditor.getController('WorkSpaceController');

			var isToolBarCreated = false;
			var onToolBarRender = controller.onToolBarRender;
			controller.onToolBarRender = function(view) {
				console.log('createMainToolBarTest: onToolBarRender');
				isToolBarCreated = true;
				verify(view.self.getName() == 'UmlWebEditor.view.toolbars.MainToolBar', 'view != MainToolBar');
			};

			var workSpaceWidget = Ext.create('UmlWebEditor.view.WorkSpace', {
				renderTo: Ext.getBody()
			});

			verify(isToolBarCreated, 'onToolBarRender not called');
			controller.onToolBarRender = onToolBarRender;

			workSpaceWidget.destroy();
		},
		createDiagrammTest: function () {
			var controller = Ext.UmlWebEditor.getController('WorkSpaceController');

			var workSpaceWidget = Ext.create('UmlWebEditor.view.WorkSpace', {
				renderTo: Ext.getBody()
			});

			var WorkSpaceMainPanel = controller.getWorkSpaceWidget();//.getComponent('WorkSpaceMainPanel');
			verify(WorkSpaceMainPanel.self.getName() == 'UmlWebEditor.view.WorkSpace', 'getWorkSpaceWidget return ' + WorkSpaceMainPanel.self.getName());

			var WorkSpaceMainPanel = WorkSpaceMainPanel.getComponent('WorkSpaceMainPanel');
			verify(WorkSpaceMainPanel.self.getName() == 'Ext.panel.Panel', 'getComponent in WorkSpace return ' + WorkSpaceMainPanel.self.getName());

			verify(WorkSpaceMainPanel.items.length >= 1, 'default diagramm not created');

			var oldLength = WorkSpaceMainPanel.items.length;
			controller.onCreateDiagramm('CommonDiagramm');
			verify(WorkSpaceMainPanel.items.length > oldLength, 'CommonDiagramm not added');

			var isOnDiagrammCreatedCalled = false;
			controller.onDiagrammCreated = function(diagramm) {
				isOnDiagrammCreatedCalled = true;
				verify(diagramm.self.getName() == 'UmlWebEditor.view.CommonDiagramm', 'created diagramm has invalid type ' + diagramm.self.getName());
			};

			oldLength = WorkSpaceMainPanel.items.length;
			controller.onCreateDiagramm('CommonDiagramm');
			verify(WorkSpaceMainPanel.items.length > oldLength, 'CommonDiagramm not added 2');

			verify(isOnDiagrammCreatedCalled, 'OnDiagrammCreated not Called');

			workSpaceWidget.destroy();
		},
		ToolBarTest: function () {
			var controller = Ext.UmlWebEditor.getController('WorkSpaceController');
		},
		chooseDiagrammTest: function () {
			var controller = Ext.UmlWebEditor.getController('WorkSpaceController');

			var workSpaceWidget = Ext.create('UmlWebEditor.view.WorkSpace', {
				renderTo: Ext.getBody()
			});
			var toolBar = controller.getMainToolBarWidget();
			var WorkSpaceMainPanel = workSpaceWidget.getComponent('WorkSpaceMainPanel');

			controller.onCreateDiagramm('CommonDiagramm');
			controller.onCreateDiagramm('CommonDiagramm');

			var visibleItem;
			var invisibleItem;
			WorkSpaceMainPanel.items.each(function (item) {
				if(item.isVisible())
					visibleItem = item;
				else
					invisibleItem = item;
			});

			toolBar.fireEvent('chooseDiagrammEvent', invisibleItem);
			WorkSpaceMainPanel.items.each(function (item) {
				if(item.isVisible())
					verify(item == invisibleItem, 'diagramm not choosed');
			});

			toolBar.fireEvent('chooseDiagrammEvent', visibleItem);
			WorkSpaceMainPanel.items.each(function (item) {
				if(item.isVisible())
					verify(item == visibleItem, 'diagramm not choosed again');
			});

			workSpaceWidget.destroy();
		},
		addBlockToDiffDiagrammTest: function () {
			var controller = Ext.UmlWebEditor.getController('WorkSpaceController');
			var commonDiagrammController = Ext.UmlWebEditor.getController('CommonDiagrammController');

			var workSpaceWidget = Ext.create('UmlWebEditor.view.WorkSpace', {
				renderTo: Ext.getBody()
			});
			var toolBar = controller.getMainToolBarWidget();
			var WorkSpaceMainPanel = workSpaceWidget.getComponent('WorkSpaceMainPanel');

			controller.onCreateDiagramm('CommonDiagramm');
			controller.onCreateDiagramm('CommonDiagramm');

			var visibleItem;
			var invisibleItem;
			WorkSpaceMainPanel.items.each(function (item) {
				if(item.isVisible())
					visibleItem = item;
				else
					invisibleItem = item;
			});
			toolBar.fireEvent('chooseDiagrammEvent', invisibleItem);
			toolBar.fireEvent('chooseDiagrammEvent', visibleItem);

			var oldItemsCount = visibleItem.items.length;
			commonDiagrammController.addBlock({
				x: 0,
				y: 0,
				type: 'SimpleBlock'
			});
			verify(oldItemsCount + 1 == visibleItem.items.length, 'block added not to visible diagramm');

			oldItemsCount = invisibleItem.items.length;
			toolBar.fireEvent('chooseDiagrammEvent', invisibleItem);
			commonDiagrammController.addBlock({
				x: 0,
				y: 0,
				type: 'SimpleBlock'
			});
			verify(oldItemsCount + 1 == invisibleItem.items.length, 'block added not to invisible diagramm' + invisibleItem.items.length + ' ' + visibleItem.items.length);

			workSpaceWidget.destroy();
		},
		saveBlocksTest: function () {
			var controller = Ext.UmlWebEditor.getController('WorkSpaceController');
			var commonDiagrammController = Ext.UmlWebEditor.getController('CommonDiagrammController');

			var workSpaceWidget = Ext.create('UmlWebEditor.view.WorkSpace', {
				renderTo: Ext.getBody()
			});
			var mainPanel = workSpaceWidget.getComponent('WorkSpaceMainPanel');
			var toolBar = controller.getMainToolBarWidget();
			var store = workSpaceWidget.getDiagrammStore();

			for(var i = 0; i< store.count();)
				store.removeAt(i);
			store.sync();
			controller.onCreateDiagramm('CommonDiagramm');
			console.log('created one diagramm');

			var diagramm = mainPanel.items.first();
			toolBar.fireEvent('chooseDiagrammEvent', diagramm);
			mainPanel.items.each(function (item){
				console.log(item.getKey());
			});	

			var lastId = diagramm.getKey();
			var oldItemsCount = diagramm.items.length;
			commonDiagrammController.addBlock({
				x: 0,
				y: 0,
				type: 'SimpleBlock'
			});
			verify(++oldItemsCount == diagramm.items.length, 'block not added to diagramm');

			workSpaceWidget.destroy();
			console.log('destroy&create');

			var workSpaceWidget = Ext.create('UmlWebEditor.view.WorkSpace', {
				renderTo: Ext.getBody()
			});
			var mainPanel = workSpaceWidget.getComponent('WorkSpaceMainPanel');
			var toolBar = controller.getMainToolBarWidget();

			var diagramm2 = mainPanel.items.first();
			mainPanel.items.each(function (item){
				console.log(item.getKey());
			});	

			diagramm2.getBlocksStore();
			toolBar.fireEvent('chooseDiagrammEvent', diagramm2);
			verify(oldItemsCount == diagramm2.items.length, 'block not loaded from db. Length=' + diagramm2.items.length + ', id1=' + lastId + ', id2=' + diagramm2.getKey() + ', oldItemsCount=' + oldItemsCount);

			workSpaceWidget.destroy();
		},
		saveBlocksToDiffDiagrammsTest: function () {
			var controller = Ext.UmlWebEditor.getController('WorkSpaceController');
			var commonDiagrammController = Ext.UmlWebEditor.getController('CommonDiagrammController');

			var workSpaceWidget = Ext.create('UmlWebEditor.view.WorkSpace', {
				renderTo: Ext.getBody()
			});
			var mainPanel = workSpaceWidget.getComponent('WorkSpaceMainPanel');
			var toolBar = controller.getMainToolBarWidget();
			var store = workSpaceWidget.getDiagrammStore();

			for(var i = 0; i< store.count();)
				store.removeAt(i);
			store.sync();

			controller.onCreateDiagramm('CommonDiagramm');
			controller.onCreateDiagramm('CommonDiagramm');

			var visibleItem, invisibleItem;
			mainPanel.items.each(function (item) {
				if(item.isVisible())
					visibleItem = item;
				else
					invisibleItem = item;
			});

			var blocksStore = Ext.create('UmlWebEditor.store.BlocksStore');
			blocksStore.load();
			for(var i = 0; i< blocksStore.count();)
				blocksStore.removeAt(i);
			blocksStore.sync();

			commonDiagrammController.addBlock({
				x: 0,
				y: 0,
				type: 'SimpleBlock'
			});
			toolBar.fireEvent('chooseDiagrammEvent', invisibleItem);
			commonDiagrammController.addBlock({
				x: 1,
				y: 1,
				type: 'SimpleBlock'
			});

			workSpaceWidget.destroy();
			console.log('destroy&create');

			var workSpaceWidget = Ext.create('UmlWebEditor.view.WorkSpace', {
				renderTo: Ext.getBody()
			});
			var toolBar = controller.getMainToolBarWidget();
			var mainPanel = workSpaceWidget.getComponent('WorkSpaceMainPanel');

			mainPanel.items.each(function (item) {
				verify(1 == item.items.length, 'second block not found on second diagramm (' + item.items.length + ')');
			});

			workSpaceWidget.destroy();
		}
	}
});
